package sharemem

import (
	"encoding/json"
	"server_cluster/common/gdconf"
	"server_cluster/common/logger"
	"server_cluster/common/protoc/pb"
	"sort"
)

type DungeonActivity struct {
	ActivityId    int32 // 活动id
	PassDungeonId int32 // 已通关地牢id
}

const (
	OpenFuncTypeActivity = 6
)

const (
	RefreshActivityDailyTaskHourOffset = 0
	RefreshActivitySignInHourOffset    = 0
)

func (a *DungeonActivity) IsUse() bool {
	return a.ActivityId != 0
}

func (a *DungeonActivity) MemInit() {
	a.ActivityId = 0
	a.PassDungeonId = 0
}

type Task struct {
	TaskId          int32 // 任务id
	State           int8  // 任务状态
	FinishCount     int32 // 完成次数
	RewardTakeCount int32 // 奖励领取次数
}

func (t *Task) IsUse() bool {
	return t.TaskId != 0
}

func (t *Task) MemInit() {
	t.TaskId = 0
	t.State = 0
	t.FinishCount = 0
	t.RewardTakeCount = 0
}

type SignInReward struct {
	RewardDay  int32 // 奖励天数
	RewardTake bool  // 是否领取奖励
}

func (s *SignInReward) IsUse() bool {
	return s.RewardDay != 0
}

func (s *SignInReward) MemInit() {
	s.RewardDay = 0
	s.RewardTake = false
}

type SignIn struct {
	ActivityId                   int32            // 活动id
	SignInCount                  int32            // 签到天数
	DailySignInRewardList        [35]SignInReward // 每日奖励列表
	TotalSignInRewardList        [10]SignInReward // 累计奖励列表
	SignInDailyRoutineRewardTake bool             // 每日常规奖励是否领取
}

func (s *SignIn) IsUse() bool {
	return s.ActivityId != 0
}

func (s *SignIn) MemInit() {
	s.SignInCount = 0
	for i := 0; i < len(s.DailySignInRewardList); i++ {
		s.DailySignInRewardList[i].MemInit()
	}
	for i := 0; i < len(s.TotalSignInRewardList); i++ {
		s.TotalSignInRewardList[i].MemInit()
	}
	s.SignInDailyRoutineRewardTake = false
}

type ZpBdReward struct {
	ItemId    int32 // 道具id
	ItemCount int32 // 道具数量
	Count     int32 // 次数
	Take      bool  // 是否领取
}

func (z *ZpBdReward) IsUse() bool {
	return z.ItemId != 0
}

func (z *ZpBdReward) MemInit() {
	z.ItemId = 0
	z.ItemCount = 0
	z.Count = 0
	z.Take = false
}

type Zp struct {
	ActivityId     int32          // 活动id
	ZpBdCount      int32          // 转盘保底计数
	ZpBdRewardList [10]ZpBdReward // 转盘保底奖励列表
	ZpTaskList     [10]Task       // 转盘任务列表
}

func (z *Zp) IsUse() bool {
	return z.ActivityId != 0
}

func (z *Zp) MemInit() {
	z.ActivityId = 0
	z.ZpBdCount = 0
	for i := 0; i < len(z.ZpBdRewardList); i++ {
		z.ZpBdRewardList[i].MemInit()
	}
	for i := 0; i < len(z.ZpTaskList); i++ {
		z.ZpTaskList[i].MemInit()
	}
}

type UserActivity struct {
	DungeonActivityList        [100]DungeonActivity // 地牢活动列表
	DailyTaskList              [30]Task             // 日常任务列表
	LastDailyTaskRefreshTime   int64                // 上次日常任务刷新时间戳 秒
	WeeklyTaskList             [30]Task             // 周常任务列表
	LastWeeklyTaskRefreshTime  int64                // 上次周常任务刷新时间戳 秒
	DailyTaskPoint             uint32               // 日常活跃点
	DailyTaskRewardPointValue  uint32               // 日常已领奖活跃点
	WeeklyTaskPoint            uint32               // 周常活跃点
	WeeklyTaskRewardPointValue uint32               // 周常已领奖活跃点
	TowerArea                  int32                // 当前爬塔区域
	TowerLevel                 int32                // 当前爬塔关卡
	TowerPassArea              int32                // 通关爬塔区域
	TowerPassLevel             int32                // 通关爬塔关卡
	TowerPassTime              int64                // 爬塔通关时间
	SignInList                 [5]SignIn            // 签到活动列表
	LastDailySignInRefreshTime int64                // 上次每日签到刷新时间戳 秒
	LastSignInRefreshTime      int64                // 上次签到刷新时间戳 秒
	ZpList                     [10]Zp               // 转盘活动列表
	LastZpTaskRefreshTime      int64                // 上次转盘任务刷新时间戳 秒
	// TODO 先临时放这儿
	CaptainRoleId      int32
	CaptainSkillRoleId int32
	CaptainSkillGroup  int32
}

func (a *UserActivity) MemInit() {
	for i := 0; i < len(a.DungeonActivityList); i++ {
		a.DungeonActivityList[i].MemInit()
	}
	for i := 0; i < len(a.DailyTaskList); i++ {
		a.DailyTaskList[i].MemInit()
	}
	a.LastDailyTaskRefreshTime = 0
	for i := 0; i < len(a.WeeklyTaskList); i++ {
		a.WeeklyTaskList[i].MemInit()
	}
	a.LastWeeklyTaskRefreshTime = 0
	a.DailyTaskPoint = 0
	a.DailyTaskRewardPointValue = 0
	a.WeeklyTaskPoint = 0
	a.WeeklyTaskRewardPointValue = 0
	a.TowerArea = 0
	a.TowerLevel = 0
	a.TowerPassArea = 0
	a.TowerPassLevel = 0
	a.TowerPassTime = 0
	for i := 0; i < len(a.SignInList); i++ {
		a.SignInList[i].MemInit()
	}
	a.LastDailySignInRefreshTime = 0
	a.LastSignInRefreshTime = 0
	for i := 0; i < len(a.ZpList); i++ {
		a.ZpList[i].MemInit()
	}
	a.LastZpTaskRefreshTime = 0
	a.CaptainRoleId = 0
	a.CaptainSkillRoleId = 0
	a.CaptainSkillGroup = 0
}

func (a *UserActivity) SetTowerArea(towerArea int32) {
	a.TowerArea = towerArea
}

func (a *UserActivity) SetCaptainRoleId(roleId int32) {
	a.CaptainRoleId = roleId
}

func (a *UserActivity) SetLastZpTaskRefreshTime(LastZpTaskRefreshTime int64) {
	a.LastZpTaskRefreshTime = LastZpTaskRefreshTime
}
func (a *UserActivity) GetLastZpTaskRefreshTime() int64 {
	return a.LastZpTaskRefreshTime
}

func (a *UserActivity) SetLastDailyTaskRefreshTime(LastDailyTaskRefreshTime int64) {
	a.LastDailyTaskRefreshTime = LastDailyTaskRefreshTime
}
func (a *UserActivity) GetLastDailyTaskRefreshTime() int64 {
	return a.LastDailyTaskRefreshTime
}

func (a *UserActivity) SetLastWeeklyTaskRefreshTime(LastWeeklyTaskRefreshTime int64) {
	a.LastWeeklyTaskRefreshTime = LastWeeklyTaskRefreshTime
}
func (a *UserActivity) GetLastWeeklyTaskRefreshTime() int64 {
	return a.LastWeeklyTaskRefreshTime
}

func (a *UserActivity) SetLastDailySignInRefreshTime(LastDailySignInRefreshTime int64) {
	a.LastDailySignInRefreshTime = LastDailySignInRefreshTime
}
func (a *UserActivity) GetLastDailySignInRefreshTime() int64 {
	return a.LastDailySignInRefreshTime
}

func (a *UserActivity) SetLastSignInRefreshTime(LastSignInRefreshTime int64) {
	a.LastSignInRefreshTime = LastSignInRefreshTime
}
func (a *UserActivity) GetLastSignInRefreshTime() int64 {
	return a.LastSignInRefreshTime
}

func (u *User) ResetDailyTaskData() {
	for i := 0; i < len(u.UserActivity.DailyTaskList); i++ {
		u.UserActivity.DailyTaskList[i].MemInit()
	}
	u.ClearDailyTaskMap()
	u.UserActivity.DailyTaskPoint = 0
	u.UserActivity.DailyTaskRewardPointValue = 0
}

func (u *User) ResetWeeklyTaskData() {
	for i := 0; i < len(u.UserActivity.WeeklyTaskList); i++ {
		u.UserActivity.WeeklyTaskList[i].MemInit()
	}
	u.ClearWeekTaskMap()
	u.UserActivity.WeeklyTaskPoint = 0
	u.UserActivity.WeeklyTaskRewardPointValue = 0
}

func (u *User) ResetSignInData() {
	for i := 0; i < len(u.UserActivity.SignInList); i++ {
		u.UserActivity.SignInList[i].MemInit()
	}
	u.ClearSignInfoMap()
}

func (u *User) GetDungeonActivity(activityId int32) *DungeonActivity {
	activity := u.GetDungeonActivityById(activityId)
	if activity == nil {
		for i := 0; i < len(u.UserActivity.DungeonActivityList); i++ {
			activity := &(u.UserActivity.DungeonActivityList[i])
			if activity.IsUse() {
				continue
			}
			activity.ActivityId = activityId
			activity.PassDungeonId = 0
			u.AddDungeonActivity2Map(activity)
			return activity
		}
		logger.Error("no empty activity found")
		return nil
	}
	return activity
}

func (u *UserActivity) SetTowerRecord(towerConfig *gdconf.TowerConfig) {
	u.TowerArea = towerConfig.Area
	u.TowerLevel = towerConfig.Level
	u.TowerPassArea = towerConfig.Area
	u.TowerPassLevel = towerConfig.Level
}

func (u *UserActivity) UpdateTowerRecord() {
	u.TowerArea = u.TowerArea + 1
	u.TowerLevel = 0
}

func (u *UserActivity) SetTowerPassTime(time int64) {
	u.TowerPassTime = time
}

func (u *User) AddDailyTask(taskId int32) bool {
	for i := 0; i < len(u.UserActivity.DailyTaskList); i++ {
		task := &(u.UserActivity.DailyTaskList[i])
		if task.IsUse() {
			continue
		}
		task.TaskId = taskId
		task.State = int8(pb.TaskState_TASK_STATE_UNFINISH)
		task.FinishCount = 0
		u.AddDailyTask2Map(task)
		return true
	}
	logger.Error("no empty daily task found")
	return false
}

func (u *User) AddWeeklyTask(taskId int32) bool {
	for i := 0; i < len(u.UserActivity.WeeklyTaskList); i++ {
		task := &(u.UserActivity.WeeklyTaskList[i])
		if task.IsUse() {
			continue
		}
		task.TaskId = taskId
		task.State = int8(pb.TaskState_TASK_STATE_UNFINISH)
		task.FinishCount = 0
		u.AddWeeklyTask2Map(task)
		return true
	}
	logger.Error("no empty weekly task found")
	return false
}

func (u *User) TriggerActivityTask(cond int32, param ...int32) {
	logger.Debug("[TriggerActivityTask] cond: %v, param: %v, uid: %v", cond, param, u.GetUid())
	taskList := make([]*Task, 0)
	for _, task := range u.GetDailyTaskMap() {
		if task.State != int8(pb.TaskState_TASK_STATE_UNFINISH) {
			continue
		}
		taskList = append(taskList, task)
	}
	for _, task := range u.GetWeeklyTaskMap() {
		if task.State != int8(pb.TaskState_TASK_STATE_UNFINISH) {
			continue
		}
		taskList = append(taskList, task)
	}
	for _, zp := range u.GetZpMap() {
		for i := 0; i < len(zp.ZpTaskList); i++ {
			zpTask := &(zp.ZpTaskList[i])
			if !zpTask.IsUse() {
				break
			}
			zpActivityConfig := gdconf.GetZpActivityConfig(zp.ActivityId)
			if zpActivityConfig == nil {
				logger.Error("zp activity config is nil, ActivityId: %v", zp.ActivityId)
				continue
			}
			zpTaskConfig := zpActivityConfig.GetZpTask(zpTask.TaskId)
			if zpTaskConfig == nil {
				logger.Error("zp task config is nil, TaskId: %v", zpTask.TaskId)
				continue
			}
			if zpTaskConfig.RefreshType != gdconf.ZpTaskRefreshTypeResetState && zpTask.State != int8(pb.TaskState_TASK_STATE_UNFINISH) {
				continue
			}
			taskList = append(taskList, zpTask)
		}
	}
	for _, task := range taskList {
		activityTaskConfig := gdconf.GetActivityTaskConfig(task.TaskId)
		if activityTaskConfig == nil {
			continue
		}
		if cond != activityTaskConfig.FinishCond[0] {
			continue
		}
		switch cond {
		case gdconf.ActivityTaskFinishCondFinishActivityDungeon:
			if len(param) < 2 || param[0] != activityTaskConfig.FinishCond[1] {
				continue
			}
			task.FinishCount += param[1]
		case gdconf.ActivityTaskFinishCondEquipUpgrade:
			task.FinishCount++
		case gdconf.ActivityTaskFinishCondEquipAffixRemake:
			task.FinishCount++
		case gdconf.ActivityTaskFinishCondDailyTaskPoint:
			if int32(u.UserActivity.DailyTaskPoint) < activityTaskConfig.FinishCond[1] {
				continue
			}
			task.FinishCount++
		case gdconf.ActivityTaskFinishCondGacha:
			if len(param) < 1 || param[0] != activityTaskConfig.FinishCond[1] {
				continue
			}
			task.FinishCount++
		case gdconf.ActivityTaskFinishCondFinishFight:
			task.FinishCount++
		case gdconf.ActivityTaskFinishCondLogin:
			task.FinishCount++
		case gdconf.ActivityTaskFinishCondHomeGather:
			task.FinishCount += param[0]
		case gdconf.ActivityTaskFinishCondAnyDungeon:
			for _, v := range activityTaskConfig.FinishCond[1:] {
				if v != param[0] {
					continue
				}
				task.FinishCount += param[1]
				break
			}
		case gdconf.ActivityTaskFinishCondHomeAwardItem:
			task.FinishCount += param[0]
		case gdconf.ActivityTaskFinishCondEquipCompose:
			task.FinishCount += param[0]
		case gdconf.ActivityTaskFinishCondWeaponCompose:
			task.FinishCount += param[0]
		}
		if task.FinishCount >= activityTaskConfig.FinishCount {
			task.State = int8(pb.TaskState_TASK_STATE_FINISH)
		}
	}
}

func (u *User) AddSignIn(signInActivityConfig *gdconf.SignInActivityConfig) bool {
	signInfo := u.GetSignInfoById(signInActivityConfig.ActivityId)
	if signInfo != nil {
		logger.Error("sign in already exist, activityId: %v", signInActivityConfig.ActivityId)
		return false
	}
	for i := 0; i < len(u.UserActivity.SignInList); i++ {
		signIn := &(u.UserActivity.SignInList[i])
		if signIn.IsUse() {
			continue
		}
		// 签到活动数据初始化
		signIn.ActivityId = signInActivityConfig.ActivityId
		signIn.SignInCount = 0
		for index, signInConfig := range signInActivityConfig.DailyReward {
			signInReward := &(signIn.DailySignInRewardList[index])
			signInReward.RewardDay = signInConfig.RewardDay
			signInReward.RewardTake = false
		}
		for index, signInTotalReward := range signInActivityConfig.TotalReward {
			signInReward := &(signIn.TotalSignInRewardList[index])
			signInReward.RewardDay = signInTotalReward.RewardDay
			signInReward.RewardTake = false
		}
		signIn.SignInDailyRoutineRewardTake = false
		u.AddSignInfo2Map(signIn)
		return true
	}
	logger.Error("no empty sign in found")
	return false
}

func (u *User) GetDailySignInReward(activityId int32, rewardDay int32) *SignInReward {
	signIn := u.GetSignInfoById(activityId)
	if signIn == nil {
		return nil
	}
	for i := 0; i < len(signIn.DailySignInRewardList); i++ {
		signInReward := &(signIn.DailySignInRewardList[i])
		if !signInReward.IsUse() {
			continue
		}
		if signInReward.RewardDay == rewardDay {
			return signInReward
		}
	}
	return nil
}

func (u *User) GetDailySignInRewardList(activityId int32) []*SignInReward {
	signIn := u.GetSignInfoById(activityId)
	if signIn == nil {
		return nil
	}
	ret := make([]*SignInReward, 0)
	for i := 0; i < len(signIn.DailySignInRewardList); i++ {
		signInReward := &(signIn.DailySignInRewardList[i])
		if !signInReward.IsUse() {
			continue
		}
		ret = append(ret, signInReward)
	}
	return ret
}

func (u *User) GetTotalSignInReward(activityId int32, rewardDay int32) *SignInReward {
	signIn := u.GetSignInfoById(activityId)
	if signIn == nil {
		return nil
	}
	for i := 0; i < len(signIn.TotalSignInRewardList); i++ {
		signInReward := &(signIn.TotalSignInRewardList[i])
		if !signInReward.IsUse() {
			continue
		}
		if signInReward.RewardDay == rewardDay {
			return signInReward
		}
	}
	return nil
}

func (u *User) GetTotalSignInRewardList(activityId int32) []*SignInReward {
	signIn := u.GetSignInfoById(activityId)
	if signIn == nil {
		return nil
	}
	ret := make([]*SignInReward, 0)
	for i := 0; i < len(signIn.TotalSignInRewardList); i++ {
		signInReward := &(signIn.TotalSignInRewardList[i])
		if !signInReward.IsUse() {
			continue
		}
		ret = append(ret, signInReward)
	}
	return ret
}

func (u *User) DelSignActivity(activityId int32) {
	signIn := u.GetSignInfoById(activityId)
	if signIn == nil {
		logger.Error("signIn not exist, activityId: %v", activityId)
		return
	}
	u.DeleteSignInfoById(signIn.ActivityId)
	signIn.MemInit()
}

func (u *User) DelZpActivity(activityId int32) {
	zp := u.GetZpById(activityId)
	if zp == nil {
		logger.Error("zp not exist, activityId: %v", activityId)
		return
	}
	u.DeleteZpById(zp.ActivityId)
	zp.MemInit()
}

func (u *User) AddZpActivity(zpActivityConfig *gdconf.ZpActivityConfig) bool {
	data, _ := json.Marshal(zpActivityConfig)
	logger.Debug("add zp activity: %v", string(data))
	zp := u.GetZpById(zpActivityConfig.ActivityId)
	if zp != nil {
		logger.Error("zp already exist, activityId: %v", zpActivityConfig.ActivityId)
		return false
	}
	for i := 0; i < len(u.UserActivity.ZpList); i++ {
		zp := &(u.UserActivity.ZpList[i])
		if zp.IsUse() {
			continue
		}
		// 转盘活动数据初始化
		zp.ActivityId = zpActivityConfig.ActivityId
		zp.ZpBdCount = 0
		if len(zpActivityConfig.ZpBdRewardList) > len(zp.ZpBdRewardList) {
			logger.Error("zp bd reward limit")
			return false
		}
		for index, zpBdReward := range zpActivityConfig.ZpBdRewardList {
			zp.ZpBdRewardList[index].ItemId = zpBdReward.ItemId
			zp.ZpBdRewardList[index].ItemCount = zpBdReward.ItemCount
			zp.ZpBdRewardList[index].Count = zpBdReward.Count
			zp.ZpBdRewardList[index].Take = false
		}
		if len(zpActivityConfig.ZpTaskList) > len(zp.ZpTaskList) {
			logger.Error("zp task limit")
			return false
		}
		for index, zpTask := range zpActivityConfig.ZpTaskList {
			zp.ZpTaskList[index].TaskId = zpTask.TaskId
			zp.ZpTaskList[index].State = int8(pb.TaskState_TASK_STATE_UNFINISH)
			zp.ZpTaskList[index].FinishCount = 0
			zp.ZpTaskList[index].RewardTakeCount = 0
		}
		u.AddZp2Map(zp)
		return true
	}
	logger.Error("no empty zp found")
	return false
}

func (t *Task) PacketPbTask() *pb.Task {
	taskConfig := gdconf.GetActivityTaskConfig(t.TaskId)
	if taskConfig == nil {
		return nil
	}
	return &pb.Task{
		TaskId:        t.TaskId,
		Type:          pb.TaskType(taskConfig.Type),
		State:         pb.TaskState(t.State),
		CurrProgress:  int32(t.FinishCount),
		TotalProgress: taskConfig.FinishCount,
	}
}

func (u *User) PacketPbTaskData(taskType pb.TaskType) *pb.TaskData {
	if taskType == pb.TaskType_TASK_TYPE_DAILY {
		pbTaskList := make([]*pb.Task, 0)
		for _, task := range u.GetDailyTaskMap() {
			pbTask := task.PacketPbTask()
			if pbTask == nil {
				continue
			}
			pbTaskList = append(pbTaskList, pbTask)
		}
		sort.Slice(pbTaskList, func(i, j int) bool {
			if pbTaskList[i].State != pbTaskList[j].State {
				return pbTaskList[i].State < pbTaskList[j].State
			}
			if pbTaskList[i].TaskId != pbTaskList[j].TaskId {
				return pbTaskList[i].TaskId < pbTaskList[j].TaskId
			}
			return true
		})
		return &pb.TaskData{
			Type:                 pb.TaskType_TASK_TYPE_DAILY,
			TaskPoint:            int32(u.UserActivity.DailyTaskPoint),
			TaskRewardPointValue: int32(u.UserActivity.DailyTaskRewardPointValue),
			TaskList:             pbTaskList,
		}
	} else {
		pbTaskList := make([]*pb.Task, 0)
		for _, task := range u.GetWeeklyTaskMap() {
			pbTask := task.PacketPbTask()
			if pbTask == nil {
				continue
			}
			pbTaskList = append(pbTaskList, pbTask)
		}
		sort.Slice(pbTaskList, func(i, j int) bool {
			if pbTaskList[i].State != pbTaskList[j].State {
				return pbTaskList[i].State < pbTaskList[j].State
			}
			if pbTaskList[i].TaskId != pbTaskList[j].TaskId {
				return pbTaskList[i].TaskId < pbTaskList[j].TaskId
			}
			return true
		})
		return &pb.TaskData{
			Type:                 pb.TaskType_TASK_TYPE_WEEKLY,
			TaskPoint:            int32(u.UserActivity.WeeklyTaskPoint),
			TaskRewardPointValue: int32(u.UserActivity.WeeklyTaskRewardPointValue),
			TaskList:             pbTaskList,
		}
	}
}

func (u *User) PacketPbSignInInfoList() []*pb.SignInInfo {
	pbSignInInfoList := make([]*pb.SignInInfo, 0)
	for _, signIn := range u.GetSignInfoMap() {
		pbDailyRewardList := make([]*pb.SignReward, 0)
		for _, signInReward := range u.GetDailySignInRewardList(signIn.ActivityId) {
			pbDailyRewardList = append(pbDailyRewardList, &pb.SignReward{
				RewardDay:  signInReward.RewardDay,
				RewardTake: signInReward.RewardTake,
			})
		}
		pbTotalRewardList := make([]*pb.SignReward, 0)

		for _, signInReward := range u.GetTotalSignInRewardList(signIn.ActivityId) {
			pbTotalRewardList = append(pbTotalRewardList, &pb.SignReward{
				RewardDay:  signInReward.RewardDay,
				RewardTake: signInReward.RewardTake,
			})
		}
		pbSignInInfoList = append(pbSignInInfoList, &pb.SignInInfo{
			ActivityId:             signIn.ActivityId,
			SignInCount:            signIn.SignInCount,
			DailyRewardList:        pbDailyRewardList,
			TotalRewardList:        pbTotalRewardList,
			DailyRoutineRewardTake: signIn.SignInDailyRoutineRewardTake,
		})
	}
	return pbSignInInfoList
}

func (u *User) PacketPbZpInfoList() []*pb.ZpInfo {
	pbZpInfoList := make([]*pb.ZpInfo, 0)
	for _, zp := range u.GetZpMap() {
		pbZpInfo := zp.PacketPbZpInfo()
		if pbZpInfo == nil {
			continue
		}
		pbZpInfoList = append(pbZpInfoList, pbZpInfo)
	}
	return pbZpInfoList
}

func (z *Zp) PacketPbZpInfo() *pb.ZpInfo {
	zpActivityConfig := gdconf.GetZpActivityConfig(z.ActivityId)
	if zpActivityConfig == nil {
		return nil
	}
	pbZpRewardList := make([]*pb.ZpReward, 0)
	for _, zpReward := range zpActivityConfig.ZpRewardList {
		pbZpRewardList = append(pbZpRewardList, &pb.ZpReward{
			ItemId:    zpReward.ItemId,
			ItemCount: zpReward.ItemCount,
			Index:     zpReward.Index,
		})
	}
	pbZpBdRewardList := make([]*pb.ZpBdReward, 0)
	for _, zpBdReward := range z.ZpBdRewardList {
		if !zpBdReward.IsUse() {
			break
		}
		pbZpBdRewardList = append(pbZpBdRewardList, &pb.ZpBdReward{
			ItemId:    zpBdReward.ItemId,
			ItemCount: zpBdReward.ItemCount,
			Count:     zpBdReward.Count,
			IsTake:    zpBdReward.Take,
		})
	}
	return &pb.ZpInfo{
		ActivityId:     z.ActivityId,
		ZpRewardList:   pbZpRewardList,
		ZpBdRewardList: pbZpBdRewardList,
		ZpBdCount:      z.ZpBdCount,
		OnceCostItem:   &pb.Item{Id: zpActivityConfig.OnceCostItem.ItemId, Num: zpActivityConfig.OnceCostItem.ItemCount},
		ProbRuleId:     zpActivityConfig.ProbRuleId,
		TaskList:       z.PacketPbZpTask(),
		ZpBigReward:    zpActivityConfig.ZpBigReward,
	}
}

func (z *Zp) PacketPbZpTask() []*pb.Task {
	zpActivityConfig := gdconf.GetZpActivityConfig(z.ActivityId)
	if zpActivityConfig == nil {
		return nil
	}
	pbZpTaskList := make([]*pb.Task, 0)
	for _, zpTask := range z.ZpTaskList {
		if !zpTask.IsUse() {
			break
		}
		zpTaskConfig := zpActivityConfig.GetZpTask(zpTask.TaskId)
		if zpTaskConfig == nil {
			logger.Error("z task config is nil, TaskId: %v", zpTask.TaskId)
			continue
		}
		pbZpTaskList = append(pbZpTaskList, &pb.Task{
			TaskId:          zpTask.TaskId,
			Type:            pb.TaskType_TASK_TYPE_ZP,
			State:           pb.TaskState(zpTask.State),
			CurrProgress:    int32(zpTask.FinishCount),
			TotalProgress:   zpTaskConfig.FinishCount,
			RewardId:        zpTaskConfig.RewardId,
			RewardCount:     zpTaskConfig.RewardCount,
			RewardTakeCount: int32(zpTask.RewardTakeCount),
			ActivityId:      z.ActivityId,
		})
	}
	return pbZpTaskList
}
